/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = t_cassandra_detect.c                               */
/*  NOTE      =                                                    */
/*  DATE      = 2016/04/1 by jikw                                  */
/*******************************************************************/

#include <CUnit/Basic.h>
#include <stdlib.h>

#include <cassandra/cassandra_core.h>
#include "frcomm.h"
#include "frmem.h"
#include "conntrack.h"
#include "frct_comm.h"
#include "../../../src/module/cassandra/config.h"
#include "apmtest_cunit.h"

extern int cassandra_detect(Conntrack *p_st_conn, Apm_packet *p_st_packet);

static int __suite_init(void)
{

	return 0;
}

static int __suite_cleanup(void)
{

	return 0;
}

static void case1(void)
{
    int i_ret = 0;
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_packet = NULL;

    i_ret = cassandra_detect(p_st_conn, p_st_packet);

    CU_ASSERT_EQUAL(i_ret, APM_L7_IS_NO);

    return;
}

static void case2(void)
{
    int i_ret = 0;
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_packet = NULL;

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    if(p_st_conn == NULL)
        goto ERROR;
    memset(p_st_conn, 0x00, sizeof(Conntrack));

    p_st_packet = (Apm_packet *)frmalloc(sizeof(Apm_packet));
    if(p_st_packet == NULL)
        goto ERROR;
    memset(p_st_packet, 0x00, sizeof(Apm_packet));
    p_st_packet->p_payload = (char *)"\x05\x00\x00\x37\x05\x00\x00\x00\x00";
    p_st_packet->ui_payload_len = 9;

    i_ret = cassandra_detect(p_st_conn, p_st_packet);

    CU_ASSERT_EQUAL(i_ret, APM_L7_IS_NO);

    frfree(p_st_conn);
    frfree(p_st_packet);

    return;

ERROR:
    if(p_st_conn)
        frfree(p_st_conn);
    if(p_st_packet)
        frfree(p_st_packet);

    return;
}

static void case3(void)
{
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_packet = NULL;
    int i_ret = 0;

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    if(p_st_conn == NULL)
        goto ERROR;
    memset(p_st_conn, 0x00, sizeof(Conntrack));

    p_st_packet = (Apm_packet *)frmalloc(sizeof(Apm_packet));
    if(p_st_packet == NULL)
        goto ERROR;
    memset(p_st_packet, 0x00, sizeof(Apm_packet));
    p_st_packet->p_payload = (char *)"\x04\x03\x00\x37\x05\x00\x00\x00\x00";
    p_st_packet->ui_payload_len = 9;

    i_ret = cassandra_detect(p_st_conn, p_st_packet);

    CU_ASSERT_EQUAL(i_ret, APM_L7_IS_NO);

    frfree(p_st_conn);
    frfree(p_st_packet);

    return;

ERROR:
    if(p_st_conn)
        frfree(p_st_conn);
    if(p_st_packet)
        frfree(p_st_packet);

    return;
}

static void case4(void)
{
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_packet = NULL;
    int i_ret = 0;

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    if(p_st_conn == NULL)
        goto ERROR;
    memset(p_st_conn, 0x00, sizeof(Conntrack));

    p_st_packet = (Apm_packet *)frmalloc(sizeof(Apm_packet));
    if(p_st_packet == NULL)
        goto ERROR;
    memset(p_st_packet, 0x00, sizeof(Apm_packet));
    p_st_packet->p_payload = (char *)"\x04\x00\x00\x37\x02\x00\x00\x00\x00";
    p_st_packet->ui_payload_len = 9;

    i_ret = cassandra_detect(p_st_conn, p_st_packet);

    CU_ASSERT_EQUAL(i_ret, APM_L7_IS_NO);

    frfree(p_st_conn);
    frfree(p_st_packet);

    return;

ERROR:
    if(p_st_conn)
        frfree(p_st_conn);
    if(p_st_packet)
        frfree(p_st_packet);

    return;
}

static void case5(void)
{
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_packet = NULL;
    int i_ret = 0;

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    if(p_st_conn == NULL)
        goto ERROR;
    memset(p_st_conn, 0x00, sizeof(Conntrack));

    p_st_packet = (Apm_packet *)frmalloc(sizeof(Apm_packet));
    if(p_st_packet == NULL)
        goto ERROR;
    memset(p_st_packet, 0x00, sizeof(Apm_packet));
    p_st_packet->p_payload = (char *)"\x04\x00\x00\x37\x05\x00\x00\x00\x00";
    p_st_packet->ui_payload_len = 9;

    i_ret = cassandra_detect(p_st_conn, p_st_packet);

    CU_ASSERT_EQUAL(i_ret, APM_L7_IS_YES);

    frfree(p_st_conn);
    frfree(p_st_packet);

    return;

ERROR:
    if(p_st_conn)
        frfree(p_st_conn);
    if(p_st_packet)
        frfree(p_st_packet);

    return;
}

static void case6(void)
{
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_packet = NULL;
    int i_ret = 0;

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    if(p_st_conn == NULL)
        goto ERROR;
    memset(p_st_conn, 0x00, sizeof(Conntrack));

    p_st_packet = (Apm_packet *)frmalloc(sizeof(Apm_packet));
    if(p_st_packet == NULL)
        goto ERROR;
    memset(p_st_packet, 0x00, sizeof(Apm_packet));
    p_st_packet->p_payload = (char *)"\x84\x00\x00\x01\x02\x00\x00\x00\x00";
    p_st_packet->ui_payload_len = 9;

    i_ret = cassandra_detect(p_st_conn, p_st_packet);

    CU_ASSERT_EQUAL(i_ret, APM_L7_IS_NO);

    frfree(p_st_conn);
    frfree(p_st_packet);

    return;

ERROR:
    if(p_st_conn)
        frfree(p_st_conn);
    if(p_st_packet)
        frfree(p_st_packet);

    return;
}

int add_suite_cassandra_detect(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" cassandra_detect()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    if (CU_add_test(pSuite, "case1", case1) == NULL ||
        CU_add_test(pSuite, "case2", case2) == NULL ||
        CU_add_test(pSuite, "case3", case3) == NULL ||
        CU_add_test(pSuite, "case4", case4) == NULL ||
        CU_add_test(pSuite, "case5", case5) == NULL ||
        CU_add_test(pSuite, "case6", case6) == NULL)
        return FR_FAIL;

    return FR_SUCC;
}
